﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using MySql.Data.MySqlClient;

namespace project.UI.sdudent
{
    public partial class MyInfo : UIPage
    {
        private string old_id;
        public MyInfo()
        {
            InitializeComponent();
            this.Size = new System.Drawing.Size(1271, 631);
            string sqlstr = "select * from student where " +
                            $"number='{LoginHelper.whoami}';";
            MySqlDataReader rdr;
            rdr=SqlHelper.ExcuteReader(sqlstr);
            if(rdr != null)
            {
                while (rdr.Read())
                {
                    uiTextBox1.Text = rdr.GetString(0);
                    uiTextBox2.Text = rdr.GetString(1);
                    uiTextBox3.Text = rdr.GetString(2);
                    uiTextBox4.Text = rdr.GetString(3);
                    uiTextBox5.Text = rdr.GetString(4);
                }
                old_id = uiTextBox1.Text;
            }
            else
            {
                MessageBox.Show("获取信息失败", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            //确认
            string id = uiTextBox1.Text;
            string pwd = uiTextBox2.Text;
            string name = uiTextBox3.Text;
            string dept = uiTextBox4.Text;
            string phone = uiTextBox5.Text;
            string sqlstr = $"update student set " +
                $"number='{id}'," +
                $"pwd='{pwd}'," +
                $"name='{name}'," +
                $"dept='{dept}'," +
                $"phone_number='{phone}'" +
                $"where number='{old_id}'; ";


            if (string.IsNullOrEmpty(id) ||
                string.IsNullOrEmpty(pwd) ||
                string.IsNullOrEmpty(name) ||
                string.IsNullOrEmpty(dept) ||
                string.IsNullOrEmpty(phone))
            {
                MessageBox.Show("存在空数据，请检查后再修改", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            int reslut = SqlHelper.ExecuteNonQuery(sqlstr);
            if (reslut == 1)
            {

                MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else
            {
                MessageBox.Show("修改失败", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
